package be.khleuven.screencasting.facade;

import be.khleuven.screencasting.entiteiten.Persoon;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
/**
 *
 * @author Stein
 */
@Stateless
public class ScreencastFacade implements ScreencastFacadeRemote {

    @PersistenceContext()
    private EntityManager em;

    @PostConstruct
    public void load() {
        Persoon p = new Persoon();
        p.setVoornaam("Gebruiker");
        em.persist(p);
        System.out.println("Test EJB");
    }

    public Object create(Object o) {
        em.persist(o);
        return o;
    }

    public Object edit(Object o) {
        return em.merge(o);
    }

    public void remove(Object o) {
        em.remove(em.merge(o));
    }

//    public List<Object> findAll() {
//        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
//        cq.select(cq.from(Screencast.class));
//        return em.createQuery(cq).getResultList();
//        return null;
//    }

//    public int count() {
//        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
//        Root<Screencast> rt = cq.from(Screencast.class);
//        cq.select(em.getCriteriaBuilder().count(rt));
//        Query q = em.createQuery(cq);
//        return ((Long) q.getSingleResult()).intValue();
//        return 0;
//    }

//    public Object find(long id) {
//        return em.find(Object.class, id);
//    }

}